<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>综合服务系统--试卷库编辑</title>
    <!--公用样式-->
    <link rel="stylesheet" type="text/css" href="${re.contextPath}/plugin/common/css/comment.css"/>
    <link rel="stylesheet" type="text/css" href="${re.contextPath}/plugin/supplier/common/css/style.css"/>
    <!--Layui-->
    <link rel="stylesheet" href="${re.contextPath}/plugin/layui/css/layui.css" media="all"/>
    <!--extended-->
    <link rel="stylesheet" type="text/css" href="${re.contextPath}/plugin/common/css/extended.css"/>
    <link rel="stylesheet" href="/plugin/it/common/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <!--公用样式JS-->
    <script type="text/javascript" src="${re.contextPath}/plugin/jquery/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="${re.contextPath}/plugin/common/js/comment.js"></script>
    <script type="text/javascript" src="${re.contextPath}/plugin/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${re.contextPath}/plugin/zTree/js/jquery.ztree.all.js" charset="utf-8"></script>
</head>
<style>
    .Right {
        width: 100%;
        overflow-y: auto;
    }

    .screening ul li:nth-child(1) select, .screening ul li:nth-child(3) select, .screening ul li:nth-child(6) select {
        width: 30%;
    }

    .layui-btns {
        display: inline-block;
        height: 38px;
        line-height: 38px;
        padding: 0 18px;
        background-color: #fff;
        color: #00a0e9;
        border-color: #00a0e9;
        border-width: 1px;
        white-space: nowrap;
        text-align: center;
        font-size: 14px;
        border-radius: 5px;
        cursor: pointer;
        float: right;
        margin-right: 14%;
    }

    .btns {
        width: 100px;
        text-align: center;
        padding: 5px 20px;
        border-radius: 5px;
    }

    .layui-btn-normal {
        background: #5775EF;
    }

    #mask {
        z-index: 1;
        width: 100%;
        background-color: rgba(0, 0, 0, .3);
        position: absolute;
    }

    .layui-laypage .layui-laypage-curr .layui-laypage-em {
        background: #5775EF;
    }

    .layui-btn-primary:hover {
        border-color: #5775EF;
        color: #5775EF;
    }

    .layui-breadcrumb a:hover {
        color: #5775EF !important;
    }
</style>

<body>
<!-- 遮罩层 -->
<div id="mask" style=""></div>

<!--头部-->
<div class="bread-crumbs" style="background-color: #F1F4FB;padding-left: 20px;">
    <span class="layui-breadcrumb" lay-separator=">">
        <a><cite>考试管理</cite></a>
        <a href="/ex/exPaper/sjkIndex"><cite>试卷库</cite></a>
        <a href="#"><cite>修改试卷</cite></a>
    </span>
</div>
<!--容器-->
<div class="container clearfix">
    <div class="Right fr">
        <form class="layui-form" enctype="multipart/form-data">
            <div class="screening screeningOne whitebg clearfix" style="margin-top: 0;">
                <input hidden="hidden" name="paperId" id="hiddId" value="${eq.paperId}">
                <div class="bold">试卷设置</div>
                <div>
                    <label><span class="red">* </span>试卷名称：</label>
                    <#if '${eq.status}' == '002'>
                    <input value="${eq.paperName}" id="paperName" name="paperName" lay-maxlength="3" disabled = 'disabled'
                           type="text"
                           style="border: 1px solid #eee;" lay-verify="required"/>
                    <#else>
                    <input value="${eq.paperName}" id="paperName" name="paperName" lay-maxlength="3"
                           type="text"
                           style="border: 1px solid #eee;" lay-verify="required"/>
                    </#if>
                </div>
                <ul class="clearfix" style="width: 100%;margin-top: 10px">
                    <li style="width: 25%;" hidden>
                        <label class=""><span class="red">* </span>试卷分类：</label>
                        <#if '${eq.status}' == '002'>
                            <select value="${eq.paperSort}" name="paperSort" class="ml8 " id="paperSort"
                                    lay-verify="required" disabled = 'disabled'
                                    lay-ignore style="width: 54%;">
                                <option value="">请选择</option>
                                <#list sjfl as vo>
                                    <option value="${vo.badtCode}"
                                            <#if (eq.paperSort)??>
                                                <#if (eq.paperSort) ==(vo.badtCode)>
                                                    selected=selected
                                                </#if>
                                            </#if>
                                    >${vo.badtName}</option>
                                </#list>
                            </select>
                            <#else>
                                <select value="${eq.paperSort}" name="paperSort" class="ml8 " id="paperSort"
                                        lay-verify="required"
                                        lay-ignore style="width: 54%;">
                                    <option value="">请选择</option>
                                    <#list sjfl as vo>
                                        <option value="${vo.badtCode}"
                                                <#if (eq.paperSort)??>
                                                    <#if (eq.paperSort) ==(vo.badtCode)>
                                                        selected=selected
                                                    </#if>
                                                </#if>
                                        >${vo.badtName}</option>
                                    </#list>
                                </select>
                        </#if>
                    </li>
                    <li style="width: 25%;height: 35px">
                        <label class="fl"><span class="red">* </span>试卷分类：</label>
                        <div class="layui-input-inline" style="width: 154px;height: 35px">
                            <div class="layui-unselect layui-form-select downpanel" style="height: 35px">
                                <div class="layui-select-title" style="height: 35px">
                                    <span class="layui-input layui-unselect" style="padding-top: 8px;height: 35px" id="treeclass">请选择分类</span>
                                    <i class="layui-edge"></i>
                                </div>
                                <dl class="layui-anim layui-anim-upbit">
                                    <dd>
                                        <ul id="classtree"></ul>
                                    </dd>
                                </dl>
                            </div>
                        </div>
                    </li>
                    <li style="width: 25%">
                        <label class="fl"><span class="red">* </span>试卷类型：</label>
                        <select value="${eq.paperType}" name="paperType" class="ml8 " id="paperType"
                                onchange="queryNone(this)"
                                lay-verify="required"
                                lay-ignore style="width: 54%;" disabled = 'disabled'>
                            <option value="">请选择</option>
                            <option value="001"
                                    <#if (eq.paperType)??><#if (eq.paperType) == '001'>selected="selected"</#if></#if>>
                                固定试卷
                            </option>
                            <option value="002"
                                    <#if (eq.paperType)??><#if (eq.paperType) == '002'>selected="selected"</#if></#if>>
                                随机试卷
                            </option>
                        </select>
                    </li>
                    <li style="width: 25%;">
                        <label class="fl"><span class="red">* </span>总分数：</label>
                        <input value="${eq.totalScore}" id="totalScore" name="totalScore" class="fl" lay-maxlength="3"
                               type="text"
                               style="border: 1px solid #eee;width: 54%;" disabled="true" value="0"
                               lay-verify="required"/>
                    </li>
                    <li style="width: 25%;float: right">
                        <a href="javascript:void(0)" onclick="gdst()" id="gdst"
                           style="<#if (eq.paperType)??><#if (eq.paperType) == '002'>display:none;</#if></#if>box-sizing: border-box;"
                           class="layui-btn layui-btn-primary layui-border-blue">添加固定试题</a>
                        <a href="javascript:void(0)" onclick="sjst()" id="sjst"
                           style="<#if (eq.paperType)??><#if (eq.paperType) == '001'>display:none;</#if></#if>;box-sizing: border-box;"
                           class="layui-btn layui-btn-primary layui-border-blue">添加随机试题</a>
                    </li>
                </ul>
            </div>
            <div class="project whitebg" style="margin-top: 20px">
                <table id="tableList" class="layui-hide" lay-filter="tableList"></table>
            </div>
            <div class="releaseBtn center">
                <#if "${eq.status}" == "002">
                    <button type="button" class="layui-btn layui-btn-normal white ft18 close">返回</button>
                </#if>
                <#if "${eq.status}"=="001">
                    <button type="button" class="layui-btn layui-btn-normal white ft18 close">返回</button>
                    <button type="submit" lay-filter="save" class="layui-btn layui-btn-normal bluebg white ft18 "
                            lay-submit=""
                    >保存
                    </button>
                    <button type="submit" lay-filter="submit" class="layui-btn layui-btn-normal bluebg white ft18 "
                            id="btnsubmit" lay-submit=""
                    >提交
                    </button>
                </#if>
            </div>
        </form>
    </div>

    <#--  添加随机试题弹框   -->
    <div id="tests" style="display:none; margin-top: 22px;">
        <form class="layui-form">
            <ul class="clearfix" style="width: 100%;margin-top: 10px">
                <li style="width: 90%;margin-top: 20px;margin-left: 10%">
                    <label style="width: 20%"><span class="red">* </span>试题题型：</label>
                    <select name="sttx" class="ml8 " id="sttx" lay-verify="required" style="width: 70%" lay-ignore
                            onchange="refreshQuestionSjCanUse()">
                        <option value="">请选择</option>
                        <#list sttx as vo>
                            <option value="${vo.badtCode}"
                            >${vo.badtName}</option>
                        </#list>
                    </select>
                </li>
                <li style="width: 90%;margin-top: 20px;margin-left: 10%" hidden>
                    <label style="width: 20%"><span class="red">* </span>试题分类：</label>
                    <input name="stfl" class="ml8" id="stfl" lay-verify="required" style="width: 70%" lay-ignore
                            onchange="refreshQuestionSjCanUse()" />
                </li>
                <li style="width: 90%;margin-top: 20px;margin-left: 10%">
                    <label style="width: 20%"><span class="red">* </span>试题分类：</label>
                    <div class="layui-input-inline" style="width: 500px;height: 34px;" onchange="refreshQuestionSjCanUse()">
                        <div class="layui-unselect layui-form-select downpanelStfl" style="height: 34px">
                            <div class="layui-select-title" style="height: 34px">
                                <span class="layui-input layui-unselect" style="padding-top: 8px;height: 34px" id="treeclassStfl">请选择分类</span>
                                <i class="layui-edge"></i>
                            </div>
                            <dl class="layui-anim layui-anim-upbit">
                                <dd>
                                    <ul id="classtreeStfl"></ul>
                                </dd>
                            </dl>
                        </div>
                    </div>
                </li>
                <li style="width: 90%;margin-top: 20px;margin-left: 10%">
                    <label style="width: 20%"><span class="red">* </span>试题难度：</label>
                    <select name="stnd" class="ml8 " id="stnd" lay-verify="required" style="width: 70%" lay-ignore
                            onchange="refreshQuestionSjCanUse()">
                        <option value="">请选择</option>
                        <#list stnd as vo>
                            <option value="${vo.badtCode}"
                            >${vo.badtName}</option>
                        </#list>
                    </select>
                </li>
                <li style="width: 90%;margin-top: 20px;margin-left: 10%">
                    <label style="width: 20%"><span class="red">* </span>试题数量：</label>
                    <input id="stsl" name="stsl" type="number" style="border: 2px solid #eee;width: 55%"
                           placeholder="请输入试题数量" oninput="inputOver(this)"/>
                    <span id="canUseCount" style="width: 15%">/${count}</span>
                </li>
                <li style="width: 90%;margin-top: 20px;margin-left: 10%">
                    <label style="width: 20%"><span class="red">* </span>每题分数：</label>
                    <input id="mtfs" name="mtfs" type="number" style="border: 2px solid #eee;width: 70%"
                           placeholder="请输入每题分数" oninput="inputOver(this)"/>
                </li>
            </ul>
            <div style="display: flex;justify-content: center;margin: 10px 0;">
                <div class="btns" onclick="closes()"
                     style="border: 1px solid #a8a8a8;color: #a8a8a8;margin-right: 50px;">返回
                </div>
                <div class="btns" onclick="submitedSj()"
                     style="border: 1px solid #5775EF;background-color: #5775EF;color: #FFFFFF;margin-left: 50px;">
                    提交
                </div>
            </div>
        </form>
    </div>
    <#--  添加固定试题弹框  -->
    <div id="test" style="display:none; margin-top: 22px;">
        <form id="sjForm" class="layui-form">
            <ul class="clearfix" style="width: 100%;margin-top: 10px;padding: 0 20px;box-sizing: border-box;">
                <li style="width: 20%;float: left">
                    <input id="content" name="content" class="fl" type="text" placeholder="请输入试题名搜索"
                           style="border: 1px solid #DDDDDD;width: 90%;box-sizing: border-box;height: 34px;"/>
                </li>
                <li style="width: 20%;float: left">
                    <select name="quType" class="ml8 " id="quType" lay-verify="required"
                            lay-ignore style="width: 90%;">
                        <option value="">选择试题类型</option>
                        <#list sttx as vo>
                            <option value="${vo.badtCode}"
                            >${vo.badtName}</option>
                        </#list>
                    </select>
                </li>
                <li style="width: 20%;float: left" hidden>
<#--                    <select name="quCategory" class="ml8 " id="quCategory" lay-verify="required"
                            lay-ignore style="width: 90%;">
                        <option value="">选择试题分类</option>
                        <#list stfl as vo>
                            <option value="${vo.badtCode}"
                            >${vo.badtName}</option>
                        </#list>
                    </select>-->
                    <input name="quCategory" class="ml8 " id="quCategory" lay-verify="required"
                            lay-ignore style="width: 90%;">
                    </input>
                </li>
                <li style="width: 20%;float: left;height: 34px" style="margin-right: 5px">
                    <div class="layui-form-item" style="width: 165px;">
                        <div class="layui-input-inline" style="height: 34px;width: 165px;">
                            <div class="layui-unselect layui-form-select downpanelQu" style="height: 34px;width: 165px;">
                                <div class="layui-select-title" style="height: 34px;width: 165px;" >
                                    <span class="layui-input layui-unselect" style="padding-top: 8px;height: 34px;width: 165px;" id="treeclassQu">请选择试题分类</span>
                                    <i class="layui-edge"></i>
                                </div>
                                <dl class="layui-anim layui-anim-upbit">
                                    <dd>
                                        <ul id="classtreeQu"></ul>
                                    </dd>
                                </dl>
                            </div>
                        </div>
                    </div>
                </li>
                <li style="width: 20%;float: left">
                    <select name="level" class="ml8 " id="level" lay-verify="required"
                            lay-ignore style="width: 90%;">
                        <option value="">选择试题难度</option>
                        <#list stnd as vo>
                            <option value="${vo.badtCode}"
                            >${vo.badtName}</option>
                        </#list>
                    </select>
                </li>
                <li style="width: 20%;float: left">
                    <input class="layui-btns fr" type="button" value="搜索"
                           style="background-color: #5775EF;color: #FFFFFF;margin-right: 0;height: 34px;line-height: 34px;"
                           onclick="query()">
                </li>
            </ul>
            <div class="project whitebg" style="margin-top: 0;padding-bottom: 0;">
                <table id="stList" lay-data="{id:'stList'}" class="layui-hide" lay-filter="stList"></table>
            </div>
            <ul class="clearfix" style="width: 93%;margin-top: 10px;margin: 0 auto;">
                <li style="width: 50%;float: right">
                    <label class="fl"><span class="red">* </span>已选择试题数：</label>
                    <input id="num" name="num" class="fl" type="text" style="border: 1px solid #eee;width: 45%"
                           disabled="true" value="${eq.totalNum}"/>
                </li>
                <li style="width: 50%;float: right">
                    <label class="fl"><span class="red">* </span>设置每道题分数：</label>
                    <input id="score" value="5" name="score" class="fl" lay-maxlength="3" type="number"
                           style="border: 1px solid #eee;width: 45%" oninput="inputOver(this)"/>
                </li>
            </ul>
            <div style="display: flex;justify-content: center;margin: 10px 0;">
                <div class="btns" onclick="closes()"
                     style="border: 1px solid #a8a8a8;color: #a8a8a8;margin-right: 50px;">返回
                </div>
                <div class="btns" onclick="submitedGd()"
                     style="border: 1px solid #5775EF;background-color: #5775EF;color: #FFFFFF;margin-left: 50px;">
                    提交
                </div>
            </div>
        </form>
    </div>

</div>

<script type="text/javascript" src="${re.contextPath}/plugin/tools/tool.js"></script>
<script type="text/javascript" charset="utf-8" src="${re.contextPath}/plugin/common/js/tabs.js"></script>
<script type="text/javascript">
    var list = [];
    var tableStList;
    var layer;
    var form;
    var table = null;
    var table2 = null;
    var tableIns = null;
    var tableInsTwo = null;
    var layedit;
    var element = "";
    var quArr = [];
    layui.use(['jquery', 'layer', 'table', 'form', 'laytpl', 'laydate', 'element','tree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var upload = layui.upload;
        tree = layui.tree;
        var element = layui.element;
        window.check = function (bjq) {
            layedit.build(bjq);
        };

        /*试卷分类*/
        var paperSort = "${eq.paperSort}"

        /*处理试卷分类-树显示*/
        var treeDto = [];
        /*执行查询分类*/
        $(function (){
            layerAjaxGet('/ex/exPaper/selectTypeClassify', {"paperSort":paperSort}, '', function (res) {
                if (200 == res.code){
                    treeDto = res.data;
                    tree.render({
                        elem: "#classtree",
                        data: treeDto,
                        click: function (node) {
                            /*当前选择的回显*/
                            $('#treeclass').html(node.data.title);
                            /*回显隐藏id*/
                            $('#paperSort').val(node.data.id)
                            var $select = $($(this)[0].elem).parents(".layui-form-select");
                            $select.removeClass("layui-form-selected").find(".layui-select-title span").html(node.name).end().find("input:hidden[name='selectID']").val(node.id);
                        }
                    });
                    treeChecked();
                }
            })
            /*编辑时，回显树形数据指定数据*/
            function treeChecked(){
                for (var i = 0; i < treeDto.length; i++){
                    var oneId = treeDto[i]; // 第一级目录
                    if (oneId.id == paperSort){
                        $('#treeclass').html(oneId.title);
                        $('#paperSort').val(oneId.id)
                        return;
                    }
                    if (oneId.children.length != 0){
                        for (var j = 0; j < oneId.children.length; j ++){
                            var twoId = oneId.children[j];
                            if (twoId.id == paperSort){
                                $('#treeclass').html(twoId.title);
                                $('#paperSort').val(twoId.id)
                                return;
                            }
                        }
                    }
                }
            }
        })
        /*点击输入框*/
        $(".downpanel").on("click", ".layui-select-title", function (e) {
            $(".layui-form-select").not($(this).parents(".layui-form-select")).removeClass("layui-form-selected");
            $(this).parents(".downpanel").toggleClass("layui-form-selected");
            layui.stope(e);
        }).on("click", "dl i", function (e) {
            // 点击展开菜单
            layui.stope(e);
        });
        /*这个是点击的那个*/
        $(document).on("click", function (e) {
            $(".layui-form-select").removeClass("layui-form-selected");
        });



        //监听选择框  同步已选择题数
        // table.on('checkbox(stList)', function (obj) {
        //     var items = table.checkStatus('stList')
        //     $('#num').val(items.data.length);
        // });
        tablelist();
        //保存按钮监听
        form.on('submit(save)', function (data) {
            var tabledata = layui.table.cache.tableList;
            var _aln = tabledata.length;
            if (_aln <= 0) {
                layer.msg('试题不得为空', {icon: 2});
                return false;
            }
            let totalNum = $('#num').val();
            data.field.status = '001'
            data.field.totalNum = totalNum;
            // data.field.paperDetails = tabledata;
            $.ajax({
                url: '/ex/exPaper/updatePaperSubmit',
                type: "post",
                data: JSON.stringify(data.field),
                dataType: "json",
                contentType: 'application/json',
                success: function (res) {
                    if (res.code === 1) {
                        layer.msg("保存成功", {icon: 1})
                        setTimeout(function () {
                            window.parent._showtab('5253b2f369364ceca7dbca94a85b68ca', '试卷库', '/ex/exPaper/sjkIndex');
                        }, 1000)
                    } else {
                        layer.msg(res.msg, {icon: 2});
                        return false;
                    }
                }
            });
            // layerAjax('/ex/exPaper/updatePaperByUuid', data.field, '', function (res) {
            //     if (res.code === 1) {
            //         layer.msg("保存成功", {icon: 1})
            //         setTimeout(function () {
            //             window.parent._showtab('5253b2f369364ceca7dbca94a85b68ca', '试卷库', '/ex/exPaper/sjkIndex');
            //         }, 1000)
            //     } else {
            //         layer.msg(res.msg, {icon: 2});
            //         return false;
            //     }
            // });
            return false;
        });
        //提交按钮监听
        form.on('submit(submit)', function (data) {
            var tabledata = layui.table.cache.tableList;
            var _aln = tabledata.length;
            if (_aln <= 0) {
                layer.msg('试题不得为空', {icon: 2});
                return false;
            }
            let totalNum = $('#num').val();
            data.field.status = '002'
            data.field.totalNum = totalNum;
            // data.field.paperDetails = tabledata;
            $.ajax({
                url: '/ex/exPaper/updatePaperSubmit',
                type: "post",
                data: JSON.stringify(data.field),
                dataType: "json",
                contentType: 'application/json',
                success: function (res) {
                    if (res.code === 1) {
                        layer.msg("提交成功", {icon: 1})
                        setTimeout(function () {
                            window.parent._showtab('5253b2f369364ceca7dbca94a85b68ca', '试卷库', '/ex/exPaper/sjkIndex');
                        }, 1000)
                    } else {
                        layer.msg(res.msg, {icon: 2});
                        return false;
                    }
                }
            });
            // layerAjaxGet('/ex/exPaper/updatePaperByUuid', data.field, '', function (res) {
            //     if (res.code === 1) {
            //         layer.msg("提交成功", {icon: 1})
            //         setTimeout(function () {
            //             window.parent._showtab('5253b2f369364ceca7dbca94a85b68ca', '试卷库', '/ex/exPaper/sjkIndex');
            //         }, 1000)
            //     } else {
            //         layer.msg(res.msg, {icon: 2});
            //         return false;
            //     }
            // });
            return false;
        });
    });
    //取消按钮
    $('.close').click(function () {
        setTimeout(function () {
            window.parent._showtab('cfd57bd42ac34423a04f85ce703364de', '试卷库', '/ex/exPaper/sjkIndex');
        }, 1)
    });
    $(function () {
        $('#mask').hide();
    })

    //刷新随机试题可用数量
    function refreshQuestionSjCanUse() {
        let sttx = $('#sttx').val();
        let stfl = $('#stfl').val();
        let stnd = $('#stnd').val();
        let quIds = '';
        var tableList = table.cache.tableList;
        for (let i = 0; i < tableList.length; i++) {
            quIds += tableList[i].quId;
            if (i < tableList.length - 1) {
                quIds += ',';
            }
        }
        var param = {
            quType: sttx,
            quCategory: stfl,
            level: stnd,
            quIds: quIds
        }
        layerAjaxGet('/ex/exPaper/refreshQuestionSjCanUse', param, '', function (res) {
            $('#canUseCount').text('/ ' + res);
        });
    }

    //选择试卷类型进行切换数据
    function queryNone(obj) {
        var code = $(obj).find('option:selected').val();
        var tabledata = layui.table.cache.tableList;
        var _aln = tabledata.length;
        if (code == "001") {
            $("#sjst").css("display", "none");
            $("#gdst").css("display", "block");
        }
        if (code == "002") {
            $("#gdst").css("display", "none");
            $("#sjst").css("display", "block");
        }
        if (_aln > 0) {
            var id = $("#hiddId").val()
            deleteList(id, "1","");
        }
    }

    //展示试卷包含的试题列表
    function tablelist() {
        layui.use('table', function () {
            table = layui.table;
            var id = $("#hiddId").val();
            //方法级渲染
            tableIns = table.render({
                id: 'tableList',
                elem: '#tableList',
                url: '/ex/exPaper/stkList?id=' + id,
                height: "full-230",
                page: true,
                cols: [[
                    {
                        title: '序号', width: '10%', templet: function (res) {
                            return res.LAY_INDEX;
                        }
                    },
                    {field: 'quId', title: 'ID', hide: true, sort: false},
                    {field: 'paperQuId', title: 'ID', hide: true, sort: false},
                    {field: 'content', title: '试题名称', width: '20%', sort: false},
                    {field: 'quCategory', title: '试题分类', width: '15%', sort: false},
                    {field: 'quType', title: '试题类型', width: '15%',},
                    {field: 'level', title: '试题难度', width: '10%'},
                    {field: 'score', title: '分数', width: '10%'}
                    ,
                    {
                        field: 'remark', title: '操作', width: '20%', templet: function (d) {
                            return"<a href=\"javascript:void(0);\" style=\"color: #0D9BFF\" onclick=\"openItemBankDetail(\'" + d.quId + "\',\'2\')\">预览</a> | " +
                                "<a href=\"javascript:void(0);\" style=\"color: #0D9BFF\" onclick=\"deleteList(\'" + d.paperQuId + "\',\'2\',\'"+ d.score +"\')\">删除</a>";
                        }
                    }
                ]],
                done: function (res, curr, count) {
                    tableStList = res.data;
                }
            });
        });
    }

    //删除试题
    function deleteList(id, type,score) {
        var str = "";
        if ("${eq.status}" == '002') {
            layer.msg("已提交不可删除", {icon: 2});
            return false;
        }
        if (type == 1) {
            str = "类型变更会导致数据丢失,是否继续";
        } else {
            str = "确认删除";
        }
        layer.confirm(str, {
            btn: ['确定', '取消'] //按钮
        }, function () {
/*            //获取试题列表缓存
            var oldData = table.cache.tableList;
            //遍历 移除要删除的试题
            for (let i = 0; i < oldData.length; i++) {
                let row = oldData[i];
                if (row.quId === id) {
                    oldData.splice(i, 1);
                }
            }
            //指定数据重新加载列表
            table.reload('tableList', {url: null, data: oldData}, true)
            //刷新分数和总题数
            noRequestRefreshScore();
            layer.closeAll();*/
            /*2021年12月8日13:07:34 -修改为数据库删除*/
            layerAjaxGet('/ex/exPaper/deleteList', {id: id, type: type, classify:'del',score:score}, '', function (d) {
                if (d.code == 1) {
                    refreshTotalScore();
                    refreshTotalNum();
                    table.reload('tableList');
                    layer.closeAll();
                } else {
                    layer.msg(d.msg, {icon: 2});
                    layer.closeAll();
                }
            });
        })
        return false;
    }

    //添加固定试题弹框
    function gdst() {
        $('#mask').height($('.container').height() + $('.bread-crumbs').height());
        $('#mask').show();
        $("#num").val(0);
        $("#score").val("");
        layer.open({
            type: 1,
            area: ['80%', '90%'],
            title: '添加固定试题',
            content: $("#test"),
            shade: 0,
            btn: 0,
            cancel: function (layero, index) {
                $('#mask').hide();
                layer.closeAll();
            }
        });
        tablest();
        /*点击弹出框中的试题树形分类展示*/
        var treeDtoQu = [];
        layerAjaxGet('/ex/question/selectTypeClassifyQu?identifying=1', {}, '', function (res) {
            if (200 == res.code){
                treeDtoQu = res.data;
                tree.render({
                    elem: "#classtreeQu",
                    data: treeDtoQu,
                    click: function (node) {
                        /*当前选择的回显*/
                        $('#treeclassQu').html(node.data.title);
                        /*回显隐藏id*/
                        $('#quCategory').val(node.data.id)
                    }
                });
            }
        })

        /*点击输入框*/
        $(".downpanelQu").on("click", ".layui-select-title", function (e) {
            $(".layui-form-select").not($(this).parents(".layui-form-select")).removeClass("layui-form-selected");
            $(this).parents(".downpanelQu").toggleClass("layui-form-selected");
            layui.stope(e);
        }).on("click", "dl i", function (e) {
            // 点击展开菜单
            layui.stope(e);
        });
        /*这个是点击的那个*/
        $(document).on("click", function (e) {
            $(".layui-form-select").removeClass("layui-form-selected");
        });
    }

    //窗口关闭事件
    function closes() {
        $('#mask').hide();
        layer.closeAll();
    }

    //添加固定试题
    function submitedGd() {
        var items = table.checkStatus('stList')
        // var strs = [];
        // for (let i = 0; i < items.data.length; i++) {
        //     strs.push(items.data[i].quId);
        // }
        // strs.join(',');
        var id = $("#hiddId").val();
        var score = $("#score").val();
        if (score == "") {
            layer.msg("请填写每题分数", {icon: 2});
            return false;
        }
        /*//获取试题列表缓存
        var oldData = table.cache.tableList;
        //遍历选择的新试题
        for (let i = 0; i < items.data.length; i++) {
            let row = items.data[i];
            row.score = score;
            console.log(row)
            //追加数据
            oldData.push(row);
        }*/
        /*//制定数据刷新列表
        table.reload('tableList', {url: null, data: oldData}, true)
        noRequestRefreshScore();
        layer.closeAll();
        $('#mask').hide();*/
        layerAjaxGet('/ex/exPaper/insertPaperRelGd', {
            id: id,
            strs: quArr,
            score: score,
            paperSort: paperSort,
            paperType: paperType,
            paperName: paperName,
            classify: 'edit'
        }, '', function (d) {
            if (d.code == 1) {
                $('#mask').hide();
                var num = $("#num").val();
                var x = parseInt(score) * parseInt(num);
                var s = $("#totalScore").val();
                if (s == 0) {
                    $("#totalScore").val(x);
                } else {
                    $("#totalScore").val(x + parseInt(s));
                }
                refreshTotalScore();
                refreshTotalNum();
                table.reload('tableList');
                layer.closeAll();
            } else {
                layer.msg(d.msg, {icon: 2});
            }
        });
        tablest();
    }

    //添加随机试题弹框
    function sjst() {
        $('#mask').height($('.container').height() + $('.bread-crumbs').height());
        $('#mask').show();
        refreshQuestionSjCanUse();
        layer.open({
            type: 1,
            area: ['65%', '65%'],
            title: '添加随机试题',
            content: $("#tests"),
            shade: 0,
            btn: 0,
            end: function () {
                var id = $("#hiddId").val();
                //刷新页面,
                var jsondata = {
                    id: id
                };
                table2.reload('tableList', {where: jsondata});
            },
            cancel: function (layero, index) {
                cleanSjFrom();
                $('#mask').hide();
                layer.closeAll();
            }
        });
        /*点击弹出框中的试题树形分类展示*/
        var treeDtoQu = [];
        layerAjaxGet('/ex/question/selectTypeClassifyQu', {}, '', function (res) {
            if (200 == res.code){
                treeDtoQu = res.data;
                tree.render({
                    elem: "#classtreeStfl",
                    data: treeDtoQu,
                    click: function (node) {
                        /*当前选择的回显*/
                        $('#treeclassStfl').html(node.data.title);
                        /*回显隐藏id*/
                        $('#stfl').val(node.data.id)
                    }
                });
            }
        })

        /*点击输入框*/
        $(".downpanelStfl").unbind("click").on("click", ".layui-select-title", function (e) {
            $(".layui-form-select").not($(this).parents(".layui-form-select")).removeClass("layui-form-selected");
            $(this).parents(".downpanelStfl").toggleClass("layui-form-selected");
            layui.stope(e);
        }).on("click", "dl i", function (e) {
            // 点击展开菜单
            layui.stope(e);
        });
        /*这个是点击的那个*/
        $(document).on("click", function (e) {
            $(".layui-form-select").removeClass("layui-form-selected");
        });


    }

    //提交随机试题
    function submitedSj() {
        var id = $("#hiddId").val();
        var sttx = $("#sttx").val();
        var stfl = $("#stfl").val();
        var stnd = $("#stnd").val();
        var stsl = $("#stsl").val();
        var mtfs = $("#mtfs").val();
        var oldData = table.cache.tableList;
        var quIds = '';
        for (let i = 0; i < oldData.length; i++) {
            quIds += oldData[i].quId;
            if (i < oldData.length - 1) {
                quIds += ','
            }
        }
        if (sttx == "") {
            layer.msg("请填写试题题型", {icon: 2});
            return false;
        }
        if (stfl == "") {
            layer.msg("请填写试题分类", {icon: 2});
            return false;
        }
        if (stnd == "") {
            layer.msg("请填写试题难度", {icon: 2});
            return false;
        }
        if (stsl == "") {
            layer.msg("请填写试题数量", {icon: 2});
            return false;
        }
        if (mtfs == "") {
            layer.msg("请填写每题分数", {icon: 2});
            return false;
        }
        var param = {
            paperName: $('#paperName').val(),
            paperType: $('#paperType').val(),
            paperSort: $('#paperSort').val(),
            id: id,
            quType: sttx,
            num: stsl,
            level: stnd,
            quCategory: stfl,
            score: mtfs,
            quIds: quIds,
            classify: 'edit'
        };
        //根据条件获取几个随机的试题 追加到列表中
        layerAjaxGet('/ex/exPaper/insertPaperRelSj', param, '', function (d) {
            if (d.flag) {
                // $('#mask').hide();
                // for (let i = 0; i < d.data.length; i++) {
                //     d.data[i].score = mtfs;
                //     oldData.push(d.data[i]);
                // }
                // table.reload('tableList', {url: null, data: oldData}, true)
                // noRequestRefreshScore()
                // layer.closeAll();
                // cleanSjFrom();
                $('#num').val(stsl);
                $('#mask').hide();
                let oldTotalScore = $('#totalScore').val();
                $('#totalScore').val(parseInt(oldTotalScore) + stsl * mtfs);
                layer.msg(d.msg, {icon: 1});
                table.reload('tableList');
                layer.closeAll();
                cleanSjFrom();
            } else {
                layer.msg(d.msg, {icon: 2});
            }
        });
    }

    //重置随机试题弹框表单
    function cleanSjFrom() {
        $("#sttx").val('');
        $("#stfl").val('');
        $("#stnd").val('');
        $("#stsl").val('');
        $("#mtfs").val('');
    }

    //添加固定试题时展示试题列表
    function tablest() {
        table2 = layui.table;
        form = layui.form;
        let $ = layui.$;
        let ids = [];
        let tableIds = [];
        var id = $("#hiddId").val();
        var quIds = '';
        // var tableList = table.cache.tableList;
        var tableList = new Array();
        // 根据试卷查询当前选中的数据
        layerAjaxGet('/ex/exPaper/pitchList', {id:id}, '', function (res) {
            if (200 == res.code){
                for (let i = 0; i < res.data.length; i++) {
                    var d = res.data[i].quId;
                    quIds += d;
                    if (i < res.data.length - 1) {
                        quIds += ','
                    }
                }
                //方法级渲染
                tableInsTwo = table2.render({
                        id: 'stList',
                        elem: '#stList',
                        url: '/ex/exPaper/stkListByType',
                        height: 400,
                        page: true,
                        where: {
                            checkedQuIdList: quIds
                        },
                        cols: [[
                            {checkbox: true},
                            {
                                title: '序号', width: '9%', templet: function (res) {
                                    return res.LAY_INDEX;
                                }
                            },
                            {field: 'quId', title: 'ID', hide: true, sort: false},
                            {field: 'content', title: '试题名称', width: '20%', sort: false},
                            {field: 'quCategory', title: '试题分类', width: '20%', sort: false},
                            {field: 'quType', title: '试题题型', width: '15%',},
                            {field: 'level', title: '试题难度', width: '15%'},
                            {
                                field: 'remark', title: '操作', width: '15%',
                                templet: function (d) {
                                    return "<a href=\"javascript:void(0);\" style=\"color: #0D9BFF\" onclick=\"openItemBankDetail(\'" + d.quId + "\',\'2\')\">预览</a>";
                                }
                            }
                        ]],
                        done: function (res) {

                            tableIds = res.data.map(function (val) {
                                return val.quId;
                            })

                            $.each(res.data, function (idx, val) {
                                if (ids.indexOf(val.quId) > -1) {
                                    val["LAY_CHECKED"] = 'true';
                                    let index = val['LAY_TABLE_INDEX'];
                                    $('tr[data-index=' + index + '] input[type="checkbox"]').click();
                                    form.render('checkbox');
                                }
                            })

                            let checkStatus = table.checkStatus('tableList');
                            if (checkStatus.isAll) {
                                $('.layui-table-header th[data-field="0"] input[type="checkbox"]').prop('checked', true);
                                form.render('checkbox');
                            }
                        }
                    }
                );
            }
        })



        table.on('checkbox(stList)', function (obj) {
            console.log(obj);
            if (obj.checked == true) {
                if (obj.type == 'one') {
                    ids.push(obj.data.quId);
                } else {
                    for (let i = 0; i < tableIds.length; i++) {
                        if (ids.indexOf(tableIds[i]) == -1) {
                            ids.push(tableIds[i]);
                        }
                    }
                }
            } else {
                if (obj.type == 'one') {
                    let i = ids.length;
                    while (i--) {
                        if (ids[i] == obj.data.quId) {
                            ids.splice(i, 1);
                        }
                    }
                } else {
                    let i = ids.length;
                    while (i--) {
                        if (tableIds.indexOf(ids[i]) != -1) {
                            ids.splice(i, 1);
                        }
                    }
                }
            }
            $('#num').val(ids.length);
            quArr = ids;
        })
    }

    //添加固定试题页面查询方法
    function query() {
        var id = $("#hiddId").val();
        var level = $("#level").val();
        var quCategory = $("#quCategory").val();
        var quType = $("#quType").val();
        var content = $("#content").val();
        var jsondata = {
            id: id,
            level: level,
            quCategory: quCategory,
            quType: quType,
            content: content
        };
        table2.reload('stList', {where: jsondata});
    }

    //不调用后台刷新分数 和 题数
    function noRequestRefreshScore() {
        let tableList = table.cache.tableList;
        let totalNum = tableList.length;
        let totalScore = 0;
        for (let i = 0; i < tableList.length; i++) {
            totalScore += parseInt(tableList[i].score);
        }
        $('#totalScore').val(totalScore);
        $('#num').val(totalNum);
    }

    //刷新总分数
    function refreshTotalScore() {
        layerAjaxGet('/ex/exPaper/refreshTotalScore', {paperId: $('#hiddId').val()}, '', function (res) {
            $('#totalScore').val(res);
        });
    }

    //刷新总题数
    function refreshTotalNum() {
        layerAjaxGet('/ex/exPaper/refreshTotalNum', {paperId: $('#hiddId').val()}, '', function (res) {
            $('#num').val(res);
        });
    }

    //试题预览
    function openItemBankDetail(quId) {
        // window.parent._showtab(quId, '试题预览', '/ex/exPaper/stkDetail?quId=' +quId);
        var type = "auto";
        var w = "70%";
        var h = "90%";
        url = '/ex/exPaper/stkDetail?quId=' + quId;
        layer.open({
            id: 'form-add',
            type: 2,
            area: [w, h],
            fix: false,
            maxmin: true,
            shadeClose: false,
            shade: 0.4,
            title: "试题预览",
            content: url,
            end: function () {
                /*tableUserIns.reload();*/
            }
        });
    }

    let temp = /^[1-9][0-9]*$/;
    function inputOver(obj){
        if(!temp.test(obj.value)){
            layer.msg('只能为整数');
            obj.value = '';
        }
    }

</script>
</body>

</html>
